<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <p>
        <input type="text" id="username">
    </p>
    <p>
        <input type="text" id="age">
    </p>
    <p>
        <input type="button" value="提交" id="btn">
    </p>

</body>
<script type="text/javascript">
    var btn = document.getElementById('btn');
    var username = document.getElementById('username');
    var age = document.getElementById('age');
    btn.onclick = function () {
        var xhr = new XMLHttpRequest();
        var nameValue = username.value;
        var ageValue = age.value;
        //拼接请求参数
        var params = 'username=' + nameValue + '&age=' + ageValue;
        xhr.open('post', 'http://localhost:3000/post?');
        //设置请求参数格式的类型
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        xhr.send(params);
        xhr.onload = function () {
            console.log(xhr.responseText);
        }
    }
</script>

</html>